package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"shop-common/library/serializer"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-common/utils/regular"
	"shop-sys/business"
	"shop-sys/pkg/request"
	"strings"
)

type SysLogController struct {
}

// @Tags 系统日志
// @description: 分页
// @Summary 分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param keyword query string false "请求路径"
// @Param status query int false "状态"
// @Param category query string false "分组"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/log/page [get]
func (*SysLogController) Page(ctx *gin.Context) {
	form := request.SysLogSearchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	timeScope := setWhereTimeQuery(form.Data)
	paginator := pagehelper.New().SetOffset(form).
		IsPush(timeScope != nil, timeScope).
		IsPush(form.Path != "", queryWrapper.Eq("action_url", form.Path)).
		IsPush(form.Terminal != "", queryWrapper.Eq("terminal", form.Terminal)).
		IsPush(form.Method != "", queryWrapper.Eq("request_method", form.Method)).
		IsPush(form.Ip != "", queryWrapper.Eq("action_ip", form.Ip)).
		SetFilter([]string{"id"}).Paginator()

	page, err := business.ServiceImpl().SysLogService.Page(paginator)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

func setWhereTimeQuery(data string) *queryWrapper.WherePair {
	if data == "today" {
		//今天 select * from	tz_sys_log where to_days( created_at ) = to_days( now( ) );
		return queryWrapper.Eq("to_days( created_at )", gorm.Expr("to_days( now( ) )"))
	} else if data == "yesterday" {
		//昨天 select * from tz_sys_log where to_days(now()) = 1 + to_days(created_at);
		return queryWrapper.Eq("to_days(now())", gorm.Expr("? + to_days(created_at)", 1))
	} else if data == "lately7" {
		//最近7天 select * from `tz_sys_log` where date_sub(curdate(), INTERVAL 7 DAY) <= date(created_at);
		return queryWrapper.Le("date_sub(curdate(), INTERVAL 7 DAY)", gorm.Expr("date(created_at)"))
	} else if data == "lately30" {
		//最近30天 select * from `tz_sys_log` where date_sub(curdate(), INTERVAL 30 DAY) <= date(created_at);
		return queryWrapper.Le("date_sub(curdate(), INTERVAL 30 DAY)", gorm.Expr("date(created_at)"))
	} else if data == "month" {
		//最近30天 select * from `tz_sys_log` where date_sub(curdate(), INTERVAL 30 DAY) <= date(created_at);
		return queryWrapper.Le("date_sub(curdate(), INTERVAL 30 DAY)", gorm.Expr("date(created_at)"))
	} else if data == "year" {
		//本年 select * from `tz_sys_log` where date_format(`created_at`, '%Y') = date_format(curdate() , '%Y');
		return queryWrapper.Eq("date_format(`created_at`, '%Y')", gorm.Expr("date_format(curdate() , '%Y')"))
	} else if data == "" {
		return nil
	} else if regular.IsTimeScope(data) {
		//时间范围内 select * from tz_sys_log where created_at between '2019/03/05' and '2023/03/08';
		length := strings.Index(data, "-")
		return queryWrapper.Between("created_at", gorm.Expr("?", data[:length]), gorm.Expr("?", data[length+1:]))
	} else {
		return nil
	}
}

//今天 select * from	tz_sys_log where to_days( created_at ) = to_days( now( ) );
//昨天 select * from tz_sys_log where to_days(now()) = 1 + to_days(created_at);
//最近7天 select * from `tz_sys_log` where date_sub(curdate(), INTERVAL 7 DAY) <= date(created_at);
//最近30天 select * from `tz_sys_log` where date_sub(curdate(), INTERVAL 30 DAY) <= date(created_at);
//本月 select * from `tz_sys_log` where date_format(`created_at`, '%Y%m') = date_format(curdate() , '%Y%m');
//本年 select * from `tz_sys_log` where date_format(`created_at`, '%Y') = date_format(curdate() , '%Y');
//自定义 select * from tz_sys_log where created_at between '2019/03/05' and '2023/03/08';
